﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Persistente.Postagem
{
    public class PTag
    {
        private DB_SIMETRIA _db;
        private SqlConnection _conexao = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_SIMETRIA_SQL"].ConnectionString);
        public int Salvar(string teTag)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var tbTag = new tb_tag();
                tbTag.te_tag = teTag;

                _db.AddTotb_tag(tbTag);
                _db.SaveChanges();

                return tbTag.id_tag;

            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }

        }

        public bool Remover(int idTag)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbTag = _db.tb_tag.Where(q => q.id_tag == idTag).FirstOrDefault();
                if (obTbTag == null)
                {
                    return false;
                }
                else
                {
                    _db.tb_tag.DeleteObject(obTbTag);
                    _db.SaveChanges();
                    return true;
                }

            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public tb_tag pegarTagPorId(int idTag)
        {
            try
            {
                _db = new DB_SIMETRIA();
                return _db.tb_tag.Where(q => q.id_tag == idTag).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public tb_tag pegarTagPorTeTag(string teTag)
        {
            try
            {
                _db = new DB_SIMETRIA();
                return _db.tb_tag.Where(q => q.te_tag == teTag).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

    }
}
